﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algorithms
{
    public class CommandInteperator
    {
        private List<string> _arguments = new List<string>();

        public dynamic Analysis(string[] arguments)
        {
            Initialize(arguments);

            if (_arguments.Count() == 0)
            {
                return GetInvalidCommandResult();
            }

            if (_arguments[0].ToUpper() == "/HELP" || _arguments[0].ToUpper() == "/?")
            {
                ContentCommand cComd = new ContentCommand();
                return cComd.GetContentResult("/HELP");
            }
            else if (_arguments[0].ToUpper() == "/LICENSE")
            {
                ContentCommand cComd = new ContentCommand();
                return cComd.GetContentResult("/LICENSE");
            }
            else
            {
                ApplicationInfo appInfo = ApplicationInfo.GetInstance();
                List<AlgorithmInfo> searchResult = appInfo.FindAlgorithmByName(_arguments[0]);

                if (searchResult == null)
                {
                    ErrorResult eResult = new ErrorResult();
                    eResult.Message = "Cannot find or open Plugins.xml.";

                    return eResult;
                }
                else
                {
                    if (searchResult.Count() == 1)
                    {
                        AnalyzeCommand aCommand = AnalyzeCommandBuilder.Build();

                        try
                        {
                            AnalyzeStrategyResult aResult = aCommand.Execute(searchResult[0]);
                            return aResult;
                        }
                        catch (Exception ex)
                        {
                            ErrorResult eResult = new ErrorResult();
                            eResult.Message = ex.Message;
                            return eResult;
                        }
                    }
                    else if (searchResult.Count() > 1)
                    {
                        ErrorResult eResult = new ErrorResult();

                        StringBuilder sbuilder = new StringBuilder();
                        sbuilder.Append("More than one algorithms found with specified name...");

                        foreach (AlgorithmInfo nextItem in searchResult)
                        {
                            sbuilder.Append(string.Format("\n{0}/{1}.{2}", nextItem.TargetAssembly, nextItem.TargetNamespace, nextItem.TargetAlgorithm));
                        }

                        eResult.Message = sbuilder.ToString();

                        return eResult;
                    }
                    else
                    {
                        return GetInvalidCommandResult();
                    }
                }
            }
        }

        private void Initialize(string[] arguments)
        {
            for (int i = 0; i < arguments.Length; i++)
                _arguments.Add(arguments[i]);
        }

        private ErrorResult GetInvalidCommandResult()
        {
            ErrorResult eResult = new ErrorResult();
            eResult.Message = "Invalid command name.";

            return eResult;
        }
    }
}
